﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.EntityType;
using System.ComponentModel;
using Sector7.Framework.Utility;
using Sector7.Medical.Consts;
using Sector7.Medical.Biz;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// SpecControl.xaml 的交互逻辑
    /// </summary>
    public partial class SpecControl : UserControl
    {
        #region Accessor
        /// <summary>
        /// 规格改变事件
        /// </summary>
        public event EventHandler SpecUpdated = null;
        #endregion

        #region Public functions
        public void InitializeUnitList(List<M7_Unit> usageUnitList, List<M7_Unit> packUnitList, List<M7_Unit> stockUnitList)
        {
            ClientUtil.LoadItemsSource(cbbUsageUnit, usageUnitList);
            ClientUtil.LoadItemsSource(cbbPackUnit, packUnitList);
            ClientUtil.LoadItemsSource(cbbStockUnit, stockUnitList);
        }

        public void InitializeSpecList(long drugId, List<M7_DrugSpec> specList)
        {
            m_drugId = drugId;
            m_specList = specList;
            ClientUtil.LoadItemsSource(cbbRead, m_specList);
        }

        public void SetSpec(M7_DrugSpec spec)
        {
            btnAction.IsEnabled = false;
            ClientUtil.LoadItemsSource(cbbRead, new List<M7_DrugSpec> { spec });
        }

        public void UpdateSpecId(long specId)
        {
            (cbbRead.SelectedItem as M7_DrugSpec).SpecId = specId;
        }

        public M7_DrugSpec GetSpec()
        {
            return cbbRead.SelectedItem as M7_DrugSpec;
        }
        #endregion

        #region Private functions
        private void AppendNewSpecBackground()
        {
            M7_Unit usageUnit, packUnit, stockUnit;

            if (cbbUsageUnit.SelectedItem == null && cbbUsageUnit.Text != null)
            {
                usageUnit = AppendNewUnitBackground(cbbUsageUnit);
            }
            else
            {
                usageUnit = cbbUsageUnit.SelectedItem as M7_Unit;
            }
            if (cbbPackUnit.SelectedItem == null && cbbPackUnit.Text != null)
            {
                packUnit = AppendNewUnitBackground(cbbPackUnit);
            }
            else
            {
                packUnit = cbbPackUnit.SelectedItem as M7_Unit;
            }
            if (cbbStockUnit.SelectedItem == null && cbbStockUnit.Text != null)
            {
                stockUnit = AppendNewUnitBackground(cbbStockUnit);
            }
            else
            {
                stockUnit = cbbStockUnit.SelectedItem as M7_Unit;
            }

            var newSpec = new M7_DrugSpec();
            newSpec.DrugId = m_drugId;
            newSpec.PackSpec = Convert.ToDecimal(txbPackSpec.Text);
            newSpec.StockSpec = Convert.ToDecimal(txbStockSpec.Text);
            newSpec.UsageUnitId = usageUnit.UnitId;
            newSpec.UsageUnit = usageUnit;
            newSpec.PackUnitId = packUnit.UnitId;
            newSpec.PackUnit = packUnit;
            newSpec.StockUnitId = stockUnit.UnitId;
            newSpec.StockUnit = stockUnit;

            newSpec.SpecId = Agent_STOS.AddDrugSpec(newSpec);

            ClientUtil.AppendToItemsSource<M7_DrugSpec>(cbbRead, newSpec);
        }

        private M7_Unit AppendNewUnitBackground(ComboBox comboBox)
        {
            var newUnit = new M7_Unit();
            newUnit.UnitName = comboBox.Text;
            newUnit.Flags = Convert.ToInt32(comboBox.Tag);
            newUnit.UnitId = Agent_INFS.AddUnit(newUnit);

            ClientUtil.AppendToItemsSource<M7_Unit>(comboBox, newUnit);

            return newUnit;
        }

        private void OnSpecUpdated(EventArgs e)
        {
            if (SpecUpdated != null)
            {
                SpecUpdated(this, e);
            }
        }

        private void txbSpec_TextChanged(object sender, TextChangedEventArgs e)
        {
            OnSpecUpdated(new EventArgs());
        }

        private void cbbUnit_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Convert.ToInt32((sender as ComboBox).Tag) == C_UnitFlags.PACK_UNIT)
            {
                lblPackUnit.Content = (cbbPackUnit.SelectedItem as M7_Unit).UnitName;
            }

            OnSpecUpdated(new EventArgs());
        }

        private void SetSelectedUnit(ComboBox comboBox, M7_Unit unit)
        {
            if (comboBox.Items.Count > 0)
            {
                comboBox.SelectedValue = unit.UnitId;
            }
            else
            {
                comboBox.Text = unit.UnitName;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cbbUsageUnit.Tag = C_UnitFlags.USAGE_UNIT;
            cbbPackUnit.Tag = C_UnitFlags.PACK_UNIT;
            cbbStockUnit.Tag = C_UnitFlags.STOCK_UNIT;
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            var symbol = btnAction.Content.ToString();

            if (e.Key == Key.Enter && symbol == SYMBOL_OK)
            {
                AppendNewSpecBackground();

                cbbRead.Visibility = Visibility.Visible;
                grdEdit.Visibility = Visibility.Hidden;
                btnAction.Content = SYMBOL_ADD;

                e.Handled = true;
            }
        }

        private void btnAction_Click(object sender, RoutedEventArgs e)
        {
            var symbol = btnAction.Content.ToString();

            switch (symbol)
            {
                case SYMBOL_ADD:    // 点击添加规格
                    cbbRead.Visibility = Visibility.Hidden;
                    grdEdit.Visibility = Visibility.Visible;
                    btnAction.Content = SYMBOL_OK;
                    break;
                case SYMBOL_OK:     // 点击保存规格
                    AppendNewSpecBackground();

                    cbbRead.Visibility = Visibility.Visible;
                    grdEdit.Visibility = Visibility.Hidden;
                    btnAction.Content = SYMBOL_ADD;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Structure
        public SpecControl()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private const string SYMBOL_ADD = "＋";
        private const string SYMBOL_OK = "√";

        private long m_drugId = -1;
        private List<M7_DrugSpec> m_specList = null;
        #endregion
    }
}
